<?php
/*
 * start to follow someone
 */

// authenticate user first
F3::call('authentication.php');
if (F3::exists('auth_error'))
{
  // authentication failed
  return;
}

if(!F3::exists('POST.id')){
	//we need others id to follow
	header('HTTP/1.1 500 Internal Server Error');
	header("Content-Type: application/json");
	$err = array("error_message" => "Need user id to follow someone.");
	echo json_encode($err);
	return;
}

//We have all the required fields to fill in
//check whether you have already followed this guy before

$id1 = F3::get('SESSION.user_id');
$id2 = F3::get('POST.id');
if($id1 == $id2){
	header('HTTP/1.1 500 Internal Server Error');
	header("Content-Type: application/json");
	$err = array("error_message" => "id can't add himself");
	echo json_encode($err);
	return;
}
$param = array('1'=>$id1, '2'=>$id2);

$result = DB::sql("SELECT * FROM friends where id1 = ? AND id2 = ?",$param);

if(count($result)>0){//there is more than one count, which means id1 has already followed id2
	header('HTTP/1.1 500 Internal Server Error');
	header("Content-Type: application/json");
	$err = array("error_message" => "id1 has already followed UsedID2");
	echo json_encode($err);
	return;
}

$stadus = 1;

$pa = array('1'=>$id1, '2'=>$id2, '3'=> $stadus);

$re = DB::sql("INSERT INTO friends(id1, id2, Status) VALUES (?,?,?)",$pa);

header('HTTP/1.1 200');
header("Content-Type: application/json");

?>